package view.indice;

import generated.Indice;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.BoxLayout;
import javax.swing.Box;

import model.Maker;

import java.awt.Component;

/**
 * @author C-net
 * Panel permettant la restauration des indices
 */
public class JPIndiceRestaurer extends javax.swing.JPanel implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 913766773576016060L;
	private JPIndice pere ;
	private JLabel lblTitreRestaurer ;
	private String[] tabo = {"Nom", "Description","id_Idince"};
	private DefaultTableModel model = new DefaultTableModel(null,tabo);
	private JTable jTListe = new JTable(model);
	private JScrollPane jSPListe;
	private JButton btnAnnuler;
	private Box horizontalBox;
	private Component horizontalGlue;
	private Component horizontalGlue_2;
	private Component verticalStrut;
	private Box horizontalBox_1;
	private Component horizontalGlue_3;
	private Component horizontalGlue_4;
	private Component verticalStrut_1;
	private Component verticalStrut_2;
	private Component verticalStrut_3;
	private JButton btnRestaurer;


	
	public JPIndiceRestaurer(JPIndice jpIndice) {
		super();
		this.pere = jpIndice ;
		initGUI();
	}
	
	private void initGUI() {
		try {
			setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
			{
				verticalStrut = Box.createVerticalStrut(20);
				add(verticalStrut);
			}
			{
				horizontalBox_1 = Box.createHorizontalBox();
				add(horizontalBox_1);
				{
					horizontalGlue_3 = Box.createHorizontalGlue();
					horizontalBox_1.add(horizontalGlue_3);
				}
				
				{
					lblTitreRestaurer = new JLabel();
					horizontalBox_1.add(lblTitreRestaurer);
					lblTitreRestaurer.setText("Indices Supprim\u00E9s");
					lblTitreRestaurer.setFont(new Font("Serif", Font.PLAIN, 24));
					lblTitreRestaurer.setBounds(243, 29, 242, 55);
				}
				{
					horizontalGlue_4 = Box.createHorizontalGlue();
					horizontalBox_1.add(horizontalGlue_4);
				}
			}
			{
				verticalStrut_3 = Box.createVerticalStrut(20);
				add(verticalStrut_3);
			}
			{
				jSPListe = new JScrollPane(jTListe);
				this.add(jSPListe);
				jSPListe.setBounds(0, 60, 730, 670);
				jTListe.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
				{
					verticalStrut_1 = Box.createVerticalStrut(20);
					add(verticalStrut_1);
				}
				{
					horizontalBox = Box.createHorizontalBox();
					add(horizontalBox);
					{
						horizontalGlue_2 = Box.createHorizontalGlue();
						horizontalBox.add(horizontalGlue_2);
					}
					{
					btnRestaurer = new JButton();
					horizontalBox.add(btnRestaurer);
					btnRestaurer.setText("Restaurer");
					btnRestaurer.setBounds(145, 639, 150, 25);
					
								btnAnnuler = new JButton();
								horizontalBox.add(btnAnnuler);
								btnAnnuler.setText("Retour \u00E0 la liste");
								btnAnnuler.setBounds(465, 639, 150, 25);
								{
									horizontalGlue = Box.createHorizontalGlue();
									horizontalBox.add(horizontalGlue);
								}
								btnAnnuler.addActionListener(this);
					btnRestaurer.addActionListener(this);
		}
				}
				{
					verticalStrut_2 = Box.createVerticalStrut(20);
					add(verticalStrut_2);
				}
				jTListe.getColumnModel().getColumn(2).setPreferredWidth(0);
				
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void afficherListe() {
		int i = 0;
		this.model= new DefaultTableModel(null,tabo);
		while (i < pere.getCtrIndice().getIndiceSupp().size()){
			String[] tab = {pere.getCtrIndice().getIndiceSupp().get(i).getNomIndice(), 
							pere.getCtrIndice().getIndiceSupp().get(i).getDescriptionIndice(), 
							String.valueOf(pere.getCtrIndice().getIndiceSupp().get(i).getIdIndice())};
			this.model.addRow(tab);		
			i++;
		}
		jTListe.setModel(this.model);
		jTListe.getColumnModel().getColumn(2).setMinWidth(0);
		jTListe.getColumnModel().getColumn(2).setMaxWidth(0);
		jTListe.repaint();
		
		
	}
	public String getSelectedIndice(){
		int ligne = jTListe.getSelectedRow();
		return (String) jTListe.getValueAt(ligne, 2);
		
	}
	

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnAnnuler){
			try {
				pere.retourListe();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		if (e.getSource() == btnRestaurer){
			try {
				int position = 0 ;
				int idI = Integer.parseInt(this.getSelectedIndice() );
				if (idI != -1){ ;
					Indice indice = Maker.createIndice();
					for (int j = 0 ; j < pere.getCtrIndice().getIndiceSupp().size() ; j++){
						if (  pere.getCtrIndice().getIndiceSupp().get(j).getIdIndice() == idI){
							indice = pere.getCtrIndice().getIndiceSupp().get(j);
							position = j;
						}
					}
					pere.ajouterIndice(indice.getNomIndice(), 
										indice.getDescriptionIndice(), 
										indice.getFichierIndice(),
										indice.getHeureIndice(),
										indice.getTempsIndice(),
										indice.getNiveauIndice(), 
										pere.getCtrIndice().getCtrPartie().getIDIndice());
					
					pere.getCtrIndice().getIndiceSupp().remove(position);
					model.removeRow(position);					
					jTListe.setModel(model);
					jTListe.repaint();
					pere.getjPIndiceListe().afficherListe();
					this.afficherListe();
					
					
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		
	}

}
